<?php
class FooterMenusController extends AppController{
	public $helpers = array('Html', 'Form');
	public $components = array('Session');
	
	
	
	public function beforeFilter() {
		$this->Auth->deny('index');
	}
	
	function index(){
		
		$FooterMenus = $this->FooterMenu->find('all');
		
		$this->set('FooterMenus',$FooterMenus);
	}
	
	function add(){
		$this->set('FooterMenus', $this->FooterMenu->find('all'));
		
		if ($this->request->is('post')) {
			$this->request->data['FooterMenu']['user_id'] = $this->Auth->user('id'); //Added this line 
			
			if ($this->FooterMenu->save($this->request->data)){
				$this->Session->setFlash('Your post has been saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
	}
	
	function edit($id = null){
	
	
		$this->FooterMenu->id = $id;
		
		if ($this->request->is('get')) {
			$this->request->data = $this->FooterMenu->read();
		} else {
		
			if ($this->FooterMenu->save($this->request->data)) {
				$this->Session->setFlash('Your post has been updated.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Unable to update your post.');
			}
		}
	
	}
	
	function delete($id){
		
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
		
		if ($this->FooterMenu->delete($id)) {
			$this->Session->setFlash('The Footermenu with id: ' . $id . ' has been deleted.');
			$this->redirect(array('action' => 'index'));
		}
		
	}
	
	public function isAuthorized($user) {
		// All registered users can add posts
		if ($this->action === 'add') {
			return true;
		}
		
		if ($this->action === 'index') {
			
			if (isset($user['role']) && $user['role'] === 'admin') {
				return true;
			}else{
				$this->Session->setFlash('You are not authorised user');
				$this->redirect('/users/login/');
			}
		}
		
		
		// The owner of a post can edit and delete it
		if (in_array($this->action, array('edit', 'delete'))) {
			$FooterMenuId = $this->request->params['pass'][0];
			if ($this->FooterMenu->isOwnedBy($FooterMenuId, $user['id'])) {
				return true;
			}else{
				return false;
			}
		}

		return parent::isAuthorized($user);
	}
	
}
?>